<template>
  <div>
    <el-dialog :title="saveObj.title" :visible.sync="saveObj.show">
      <el-form
        ref="form"
        :rules="rules"
        :model="formData"
        label-width="108px"
        size="small"
        class="demo-ruleForm"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="标准名称" prop="stdName">
              <el-input v-model="formData.stdName" placeholder="请输入名字" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="ph上限值" prop="phUpper">
              <el-input v-model.number="formData.phUpper" placeholder="请输入数字" type="number" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="ph下限值" prop="phLower">
              <el-input v-model="formData.phLower" placeholder="请输入数字" type="number" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="cod上限值" prop="codUpper">
              <el-input v-model="formData.codUpper" placeholder="请输入数字" type="number" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="cod下限值" prop="codLower">
              <el-input v-model="formData.codLower" placeholder="请输入数字" type="number" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="氨氮上限值" prop="nh3Upper">
              <el-input v-model="formData.nh3Upper" placeholder="请输入数字" type="number" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="氨氮下限值" prop="nh3Lower">
              <el-input v-model="formData.nh3Lower" placeholder="请输入数字" type="number" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="tn上限值" prop="tnUpper">
              <el-input v-model="formData.tnUpper" placeholder="请输入数字" type="number" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="tn下限值" prop="tnLower">
              <el-input v-model="formData.tnLower" placeholder="请输入数字" type="number" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="tp上限值" prop="tpUpper">
              <el-input v-model="formData.tpUpper" placeholder="请输入数字" type="number" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="tp下限值" prop="tpLower">
              <el-input v-model="formData.tpLower" placeholder="请输入数字" type="number" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeFn('form')">取 消</el-button>
        <el-button type="primary" @click="sumbitFn('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addSysStandard, checkSysName } from '@/api/infoConfig/waterStandard'

export default {
  props: {
    saveObj: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {},
      rules: {
        stdName: [{ required: true, message: '此为必填项', trigger: 'blur' }],
        phUpper: [
          {
            required: true,
            message: '此为必填项且只能输入数字',
            trigger: 'blur'
          }
        ],
        phLower: [
          {
            required: true,
            message: '此为必填项且只能输入数字',
            trigger: 'blur'
          }
        ],
        codUpper: [
          {
            required: true,
            message: '此为必填项且只能输入数字',
            trigger: 'blur'
          }
        ],
        codLower: [
          {
            required: true,
            message: '此为必填项且只能输入数字',
            trigger: 'blur'
          }
        ],
        nh3Upper: [
          {
            required: true,
            message: '此为必填项且只能输入数字',
            trigger: 'blur'
          }
        ],
        nh3Lower: [
          {
            required: true,
            message: '此为必填项且只能输入数字',
            trigger: 'blur'
          }
        ],
        tnUpper: [
          {
            required: true,
            message: '此为必填项且只能输入数字',
            trigger: 'blur'
          }
        ],
        tnLower: [
          {
            required: true,
            message: '此为必填项且只能输入数字',
            trigger: 'blur'
          }
        ],
        tpUpper: [
          {
            required: true,
            message: '此为必填项且只能输入数字',
            trigger: 'blur'
          }
        ],
        tpLower: [
          {
            required: true,
            message: '此为必填项且只能输入数字',
            trigger: 'blur'
          }
        ]
      },
      canSub: false,
      isEdit: true
    }
  },
  methods: {
    // 赋值表单数据
    getFormFn() {
      this.formData = this.saveObj.data
      // console.log(this.formData)
    },
    closeFn(data) {
      this.saveObj.show = false
      this.$refs[data].resetFields()
    },
    sumbitFn(data) {
      this.$refs[data].validate(valid => {
        if (valid) {
          console.log(this.formData)
          console.log(this.isEdit)
          if (!this.isEdit) {
            this.checkSysName()
          }
          if (this.isEdit) {
            this.sumbitSureFn(this.formData)
          }
        } else {
          this.$message.error('*为必填项')
        }
      })
    },
    sumbitSureFn(data) {
      addSysStandard(this.formData).then(res => {
        this.$notify({
          title: '成功',
          message: res.msg,
          type: 'success'
        })
        this.saveObj.show = false
        this.canSub = false
        this.$emit('getSysStandardFn')
      })
    },
    checkSysName() {
      checkSysName({ stdName: this.formData.stdName }).then(res => {
        if (res.data === 0) {
          this.sumbitSureFn(this.formData)
        } else {
          this.$notify.error({
            title: '错误',
            message: '名称重复，添加失败'
          })
        }
      })
    },
    // 是否是编辑
    checkIsEdit(data) {
      this.isEdit = data
    }
  }
}
</script>
